//
//  V2MainViewController.m
//  V2EX
//
//  Created by 花菜ChrisCai on 2016/6/11.
//  Copyright © 2016年 Chris. All rights reserved.
//

#import "V2MainViewController.h"
#import "V2LatestViewController.h"
#import "V2CategoriesViewController.h"
#import "V2NodesViewController.h"
#import "V2FavoriteViewController.h"
#import "V2NotificationViewController.h"
#import "V2ProfileViewController.h"
#import "V2LoginViewController.h"
#import "V2NavigationController.h"
#import "V2LeftMenuView.h"

static CGFloat const kLeftMenuWidth = 240.0;
@interface V2MainViewController ()<V2LeftMenuViewDelegate,UIGestureRecognizerDelegate>
/** 当前选中的控制器类型 */
//@property(nonatomic, assign) V2SectionType curretSelectType;
/** 容器控件 */
@property(nonatomic, weak) UIView *containView;
/** 左边菜单控件 */
@property(nonatomic, strong) V2LeftMenuView *leftMenuView;
/** 全屏背景按钮 */
@property(nonatomic, strong)  UIButton *backgroundButton;
@end

@implementation V2MainViewController

#pragma mark -
#pragma mark - 生命周期方法
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 初始化容器视图
    [self setupContainView];
    // 初始化子控制器
    [self setupAllChildViewControllers];
    // 添加全屏背景按钮
    [self setupBackgroundButton];
    // 左边菜单控件
    [self setupLeftMenuView];
    // 添加侧滑手势
    [self setupGestureRecognizer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leftClick) name:V2LeftBarButtonItemClickNotification object:nil];
    
}

#pragma mark -
#pragma mark - 初始化操作
- (void)setupGestureRecognizer
{
    UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(leftPan:)];
    edgePan.edges = UIRectEdgeLeft;
//    edgePan.delegate = self;
    [self.view addGestureRecognizer:edgePan];
}

- (void)leftPan:(UIScreenEdgePanGestureRecognizer *)edgePan
{
    CGFloat progress = [edgePan translationInView:self.view].x / kLeftMenuWidth;
    if (progress > 1) {
        progress = 1;
    }
    if (edgePan.state == UIGestureRecognizerStateBegan) {
    }else if (edgePan.state == UIGestureRecognizerStateChanged){
        [self setMenuOffset:progress * kLeftMenuWidth];
        
    }else {
        if (progress > 0.5) {
            [UIView animateWithDuration:0.25 animations:^{
                [self setMenuOffset:kLeftMenuWidth];
            }];
        }else {
            [UIView animateWithDuration:0.25 animations:^{
                [self setMenuOffset:0];
            }];
        }
    }
}

- (void)setupContainView
{
    UIView *containView = [[UIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:containView];
    self.containView = containView;
}

/**
 *  添加所有子控制器
 */
- (void)setupAllChildViewControllers
{
    V2LatestViewController *lastVc = [[V2LatestViewController alloc]init];
    [self setupOneViewController:lastVc Title:@"最新"];
    
    V2CategoriesViewController *categoriesVc = [[V2CategoriesViewController alloc]init];
    [self setupOneViewController:categoriesVc Title:@"分类"];
    
    V2NodesViewController *nodesVc = [[V2NodesViewController alloc]init];
    [self setupOneViewController:nodesVc Title:@"节点"];
    
    V2FavoriteViewController *favoriteVc = [[V2FavoriteViewController alloc]init];
    [self setupOneViewController:favoriteVc Title:@"收藏"];
    
    V2NotificationViewController *notiVc = [[V2NotificationViewController alloc]init];
    [self setupOneViewController:notiVc Title:@"提醒"];
    
    V2ProfileViewController *pofileVc = [[V2ProfileViewController alloc]init];
    [self setupOneViewController:pofileVc Title:@"个人"];
}

/**
 *  添加单个子控制器
 */
- (void)setupOneViewController:(UIViewController *)viewController Title:(NSString *)title
{
    viewController.navigationItem.title = title;
    
    V2NavigationController *nav = [[V2NavigationController alloc]initWithRootViewController:viewController];
    
    [self addChildViewController:nav];
}
/**
 *  初始化侧边栏
 */
- (void)setupLeftMenuView
{
    V2LeftMenuView *leftMenuView =  [V2LeftMenuView menuView];
    leftMenuView.delegate = self;
    leftMenuView.x = 0;
    leftMenuView.width = kLeftMenuWidth;
    leftMenuView.height = V2ScreenHeight;
    [self.view addSubview:leftMenuView];
    self.leftMenuView = leftMenuView;
    [self.leftMenuView setSelectedIndex:0];
}

/**
 *  全屏背景按钮
 */
- (void)setupBackgroundButton
{
   UIButton *backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundButton addTarget:self action:@selector(backgroundButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    backgroundButton.hidden = YES;
    backgroundButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    backgroundButton.frame = self.view.bounds;
    [self.view addSubview:backgroundButton];
    self.backgroundButton = backgroundButton;
    
}

#pragma mark -
#pragma mark - 逻辑事件
/**
 *  导航栏左边按钮点击
 */
- (void)leftClick
{
    [self setMenuOffset:kLeftMenuWidth];
}

- (void)backgroundButtonClick:(UIButton *)btn
{
    [self setMenuOffset:0];
}
/**
 *  修改左边菜单控件位置
 *
 *  @param offset x偏移
 */
- (void)setMenuOffset:(CGFloat)offset
{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.leftMenuView.x = offset - kLeftMenuWidth;
    } completion:^(BOOL finished) {
        if (self.leftMenuView.x == 0) {
            self.backgroundButton.hidden = NO;
        } else {
            self.backgroundButton.hidden = YES;
        }
    }];
}


#pragma mark -
#pragma mark - V2LeftMenuViewDelegate
- (void)menuView:(V2LeftMenuView *)menuView selectCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *wileShowVc = self.childViewControllers[indexPath.row];
    wileShowVc.view.frame = self.view.bounds;
    [self.containView addSubview:wileShowVc.view];
    [self setMenuOffset:0];
}

#pragma mark -
#pragma mark - dealloc
/**
 *  移除监听
 */
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:V2LeftBarButtonItemClickNotification object:nil];
}
@end
